package mutilthread;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 多个线程用同一个对象上wait, notify 随机唤醒一个线程，notifyAll 唤醒所有线程
 * User: Li_yizYa
 * Date: 2025—03—25
 * Time: 18:05
 */
public class Demo12 {
    private static Object Locker = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println("t1 wait 之前");
            synchronized (Locker) {
                try {
                    Locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("t1 wait 之后");
        });

        Thread t2 = new Thread(() -> {
            System.out.println("t2 wait 之前");
            synchronized (Locker) {
                try {
                    Locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("t2 wait 之后");
        });

        Thread t3 = new Thread(() -> {
            System.out.println("t3 wait 之前");
            synchronized (Locker) {
                try {
                    Locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("t3 wait 之后");
        });

        Thread t4 = new Thread(() -> {
            Scanner scan = new Scanner(System.in);
            scan.next();
            System.out.println("t4 notify 之前");
            synchronized (Locker) {
                //Locker.notify();//唤醒一个
                Locker.notifyAll();//唤醒所有
            }
            System.out.println("t4 notify 之后");
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
